#include <iostream>
#include "SVCHAlgorithmService.h"
#include "fft.h"

SVCHImpulseDTO* createSvchImpulseDTO(JNIEnv *env,jobject svchImpulseDTO){

	jclass cls = env->GetObjectClass(svchImpulseDTO); //or use FindClass("ru/ntcreb/csir/libs/methods/SVCH/SVCHImpulseDTO");
	jfieldID voltageAplitude_A_ID = env->GetFieldID(cls, "voltageAplitude_A", "D");
	jdouble voltageAplitude_A = env-> GetDoubleField(svchImpulseDTO,voltageAplitude_A_ID);

	jfieldID impulseIncreaseConstant_Alpha_ID = env->GetFieldID(cls, "impulseIncreaseConstant_Alpha", "D");
	jdouble impulseIncreaseConstant_Alpha = env-> GetDoubleField(svchImpulseDTO,impulseIncreaseConstant_Alpha_ID);

	jfieldID impulseFallingConstant_Beta_ID = env->GetFieldID(cls, "impulseFallingConstant_Beta", "D");
	jdouble impulseFallingConstant_Beta = env-> GetDoubleField(svchImpulseDTO,impulseFallingConstant_Beta_ID);

	jfieldID impulseFillingFrequency_U0_ID = env->GetFieldID(cls, "impulseFillingFrequency_U0", "D");
	jdouble impulseFillingFrequency_U0 = env-> GetDoubleField(svchImpulseDTO,impulseFillingFrequency_U0_ID);

	SVCHImpulseDTO* result = new SVCHImpulseDTO(voltageAplitude_A,impulseIncreaseConstant_Alpha,impulseFallingConstant_Beta,impulseFillingFrequency_U0);

	return result;
}

map <double,double>  createSVCHImpulse(SVCHImpulseDTO* svchImpulseDTO){
	map <double,double> result;

	for (double time=0; time < 0.0001;time+=0.00000001) {

		double Uvx=(svchImpulseDTO->voltageAplitude_A * 
			(exp(-svchImpulseDTO->impulseIncreaseConstant_Alpha*time)-exp(-svchImpulseDTO->impulseFallingConstant_Beta *time)) *
			sin(svchImpulseDTO->impulseFillingFrequency_U0 *time));

		result[time]=Uvx;

	}
	return result;
}


map <double,double> createTractSVCHReaction(map<double,double> svchImpulseVoltageMap,TractDTO tract1,TractDTO tract2){

	map <double,double> result;
	bool comprehensive_conn=true;
	double currency1,currency2 = 0;
	double step,step2 = 0;

	map<double,double>::iterator it;


	for(it = svchImpulseVoltageMap.begin();it !=svchImpulseVoltageMap.end();++it){
		if(comprehensive_conn){
		//parallel conn
		step = abs(pow(abs(it->second) - tract1.voltage_U0,
			tract1.approximationCoefficient_N));

		currency1 = pow(tract1.stepness_S,
			1 / (1 + tract1.approximationCoefficient_K * (step)));


		step2 = abs(pow(abs(it->second) - tract1.voltage_U0,
			tract1.approximationCoefficient_N));

		currency2 = pow(tract1.stepness_S,
			1 / (1 + tract1.approximationCoefficient_K * (step)));

		result[it->first] = currency1+currency2;
		}
		else{



		}
		//TODO implement comprehensive conn 


	}

	return result;

}

//not working, use fast fourier transform
double * createTractSVCHFrequencyReaction(double* tempArray,int arrLenght){

	double* result = new double[8];

	for(int i = 0 ; i<8;i++){
		result[i] =0.0;
	}

	FFT(tempArray,result,8,3,-1);

	for(int i = 0 ; i<8;i++){
		cout<<result[i]<<"re"<<endl;
		cout<<tempArray[i]<<"im"<<endl;
	}

	return result;

}